package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.facade.job.MonthSalesProgressReportSyncJob;
import com.alibaba.citrus.ots.application.report.ability.repository.SalesOrderLineRepository;
import com.alibaba.citrus.ots.common.util.DateUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.domain.orderline.dto.QueryQrderLineByCompletedByReadonlyRequest;
import com.epoch.app.crb2btradecenter.domain.orderline.dto.QueryQrderLineByCompletedRequest;
import com.epoch.app.crb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.crb2btradecenter.domain.orderline.service.OrderLineService;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.dto.PageStatisticalOrderCompleteRequest;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.dto.StatisticalSalesOrderQuantityGroupByCreateRequest;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.model.ObtcSaleOrderLineIndex;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.service.ObtcSaleOrderLineIndexService;
import com.epoch.app.otsapplicationreportsdk.contants.OtsApplicationReportSdkConstants;
import com.epoch.app.otsapplicationreportsdk.model.dto.CommPageQueryRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/23 21:56
 */
@Component
public class SalesOrderLineRepositoryImpl implements SalesOrderLineRepository {

    Log log = Log.getLogger(MonthSalesProgressReportSyncJob.class);

    @Resource
    ObtcSaleOrderLineIndexService obtcSaleOrderLineIndexService;

    @Resource
    OrderLineService orderLineService;

    /**
     * 根据创建日期，分页查询销售订单明细ES交货单数量、订单折前价格、发票金额等
     * @param request
     * @return
     */
    @Override
    public Result<List<ObtcSaleOrderLineIndex>> statisticalMonthSalesOrderCompleteData(CommPageQueryRequest request) {
        PageStatisticalOrderCompleteRequest queryRequest = PageStatisticalOrderCompleteRequest.builder()
                        .mainBisUnitCode(request.getOrgBusinessUnitCode())
                        .mainOrganizationCode(request.getOrgSalesOrganizationCode())
                        .beginDate(Long.valueOf(DateUtils.dateToStamp(request.getBeginDate())))
                        .endDate(Long.valueOf(DateUtils.dateToStamp(request.getEndDate())))
                        .start(request.getStart())
                        .limit(request.getLimit())
                        .build();
        return obtcSaleOrderLineIndexService.pageStatisticalOrderComplete(queryRequest);
    }

    /**
     * 根据修改日期，分页查询销售订单明细ES交货单数量、订单折前价格、发票金额等
     * @param request
     * @return
     */
    @Override
    public PageResult<List<ObtcSaleOrderLineIndex>> statisticalSalesOrderQuantityGroupByCreate(CommPageQueryRequest request) {
        StatisticalSalesOrderQuantityGroupByCreateRequest queryRequest = StatisticalSalesOrderQuantityGroupByCreateRequest.builder()
                .beginDate(Long.valueOf(DateUtils.dateToStamp(request.getBeginDate())))
                .endDate(Long.valueOf(DateUtils.dateToStamp(request.getEndDate())))
                .build();
        queryRequest.setStart(request.getStart());
        queryRequest.setLimit(request.getLimit());
        Result<List<ObtcSaleOrderLineIndex>> result = obtcSaleOrderLineIndexService.statisticalSalesOrderQuantityGroupByCreate(queryRequest);
        return PageResult.of(result.getResult(),result.getTotal());
    }

    /**
     * 根据创建日期、分子公司，分页查询已完成的销售订单列表db
     * @param request
     * @return
     */
    @Override
    public Result<List<OrderLine>> queryQrderLineByCompleted(CommPageQueryRequest request) {
        Result<List<OrderLine>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            QueryQrderLineByCompletedByReadonlyRequest queryRequest = QueryQrderLineByCompletedByReadonlyRequest.builder()
                    .orgSalesOrganizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = orderLineService.queryQrderLineByCompletedByReadonly(queryRequest);
        }else{
            QueryQrderLineByCompletedRequest queryRequest = QueryQrderLineByCompletedRequest.builder()
                    .orgSalesOrganizationIds(request.getOrganizationIds())
                    .beginDate(request.getBeginDate())
                    .endDate(request.getEndDate())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = orderLineService.queryQrderLineByCompleted(queryRequest);
        }
        return result;
    }


}
